/*
 * @Author: 大D
 * @email: 857216393@qq.com
 * @Date: 2024-02-01 12:44:45
 * @Description: 博客列表 数据获取 分页 跳转等 hooks
 */
import { getBlogList } from '@/apis/article';
import type { IBlog, IPagination } from '@/apis/type';
import { useRouter } from 'vue-router';

const useBlogList = () => {
  const showModal = ref(false);
  const articleList: Ref<IBlog[]> = ref([]);
  const total = ref(0);
  // 请求博客列表数据
  const pagination: Ref<IPagination> = ref({
    page: 1,
    pageSize: 10,
    categoryId: null,
    keyword: null
  });
  const getListFun = async () => {
    showModal.value = false;
    const res = await getBlogList(pagination.value);
    total.value = res.data.count;
    articleList.value = res.data.rows.map((item: IBlog) => {
      let content = item.content && item.content.replace(/<[^>]+>/gi, '');
      item.content = content && content.substring(0, 50) + '...';
      return item;
    });
  };
  getListFun();
  const pageChange = (currentPage: number, pageSize: number) => {
    pagination.value.page = currentPage;
    pagination.value.pageSize = pageSize;
    getListFun();
  };
  const $router = useRouter();
  const goDetail = (id: number | string) => {
    $router.push({ path: '/article', query: { id: id } });
  };
  return { showModal, articleList, getListFun, pagination, pageChange, total, goDetail };
};
export default useBlogList;
